package com.changgou.order.controller;

import com.changgou.order.pojo.OrderItem;
import com.changgou.order.service.CartService;
import entity.Result;
import entity.StatusCode;
import entity.TokenDecode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/****
 * 购物车
 */
@RestController
@RequestMapping("/cart")
public class CartController {

    @Autowired
    private CartService cartService;
    @Autowired
    private TokenDecode tokenDecode;

    /****
     * 添加商品到购物车
     * @param id
     * @param num
     * @return
     */
    @RequestMapping("/add")
    public Result add(Long id, Integer num) {
        //添加购物车的用户名
        String username = tokenDecode.getUserInfo().get("username");
        cartService.add(id, num, username);
        return new Result(true, StatusCode.OK, "添加购物车成功");
    }

    /***
     * 查询用户购物车列表
     * @return
     */
    @GetMapping(value = "/list")
    public Result list() {
        //用户名
        String username = tokenDecode.getUserInfo().get("username");
        List<OrderItem> orderItems = cartService.list(username);
        return new Result(true, StatusCode.OK, "购物车列表查询成功！", orderItems);
    }
}
